<template>
  <el-dialog
    :title="title"
    :before-close="handleClose"
    :visible="deviceDialogVisible"
    width="handleScreenWidthToVw(574)+'px'"
    v-loading="dialogLoading"
  >
    <el-form
      ref="form"
      :model="form"
      :label-width="handleScreenWidthToVw(120)+'px'"
      :rules='rules'
    >
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input v-model="form.enterpriseName" placeholder="请输入企业名称" class="input-width" />
      </el-form-item>
      <el-form-item label="法人代表" prop="legalRepresentative">
        <el-input v-model="form.legalRepresentative" placeholder="请输入法人代表" class="input-width" />
      </el-form-item>
      <el-form-item label="注册资本" prop="registeredCapital">
        <el-input v-model="form.registeredCapital" placeholder="请输入注册资本" class="input-width" >
            <template slot="append">万元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="办公地址" prop="businessAddress">
        <el-input  type="textarea" v-model="form.businessAddress" placeholder="请输入办公地址" class="input-width" />
      </el-form-item>
          <el-form-item label="工商分类" prop="classification">
          <el-select v-model="form.classification"  clearable filterable class="w-100P">
            <el-option
              v-for="state in netTypeList"
              :key="state.value"
              :value="state.value"
              :label="state.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="主要行业" >
          <div >
            <el-select v-model="form.industryId"   placeholder="请选择" clearable filterable class="w-100P" @change="sureChange">
              <el-option  v-for="state in options"    :key="state.id"    :value="state.id"    :label="state.name" />
            </el-select>
          </div>
          <div v-show ="twoUP">
            <el-select v-model="form.IndustryPid"  clearable filterable class="w-100P"  placeholder="请选择">
              <el-option v-for="state in options1" :key="state.id" :value="state.id" :label="state.name"/>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="股票代码" prop="stockCode">
          <el-input v-model="form.stockCode" placeholder="请输入股票代码" class="input-width" />
        </el-form-item>
        <el-form-item label="注册日期" prop="establishDate">
          <el-date-picker
            v-model="form.establishDate"
            type="date"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入园日期" prop="inputDate">
          <el-date-picker
              v-model="form.inputDate"
              type="date"
              placeholder="选择日期"
              format="yyyy-MM-dd"
              value-format="timestamp"
              clearable>
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item  label="登录账户">
          <el-input v-model="userVo.account" placeholder="请输入登录账户" class="input-width" />
        </el-form-item>
        <el-form-item label="默认密码" >
            <el-input v-model="userVo.password" class="input-width" />
        </el-form-item> -->
        <!-- <div style="text-align:center;color:#000;font-weight:600" class="mg-bottom-10">
          管理员账户
        </div> -->
        <el-form-item>
          <el-button size="mini" type="primary" plain @click="success('form')">保存</el-button>
          <el-button size="mini" type="primary" plain @click="handleClose">取消</el-button>
        </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
//  判断id 是否需要获取回显信息，需要获取 主要行业下拉框  保存需要调用保存接口 修改接口
//   import { apiSetOperateSpeed } from '@/views/device/create-product/api'
import { handleScreenWidthToVw } from '@/utils/tools'
import { ApiEnterpriseInfo,AddEnterprise,UpdateEnterprise } from '../api' // addUserOrPassword, updateUserOrPassword
 import { getUpDataList } from '@/api/Mycompany/index'
export default {
    props: {
        title: { type: String, default: '新增' },
        deviceDialogVisible: { type: Boolean, default: false },
        AddId: { type: String, default: ''}
    },
    data() {
      return {
        netTypeList: [{
          value: '个人独资',
          label: '个人独资'
        }, {
          value: '个体工商户',
          label: '个体工商户'
        }, {
          value: '有限公司',
          label: '有限公司'
        }, {
          value: '责任有限公司',
          label: '责任有限公司'
        }, {
          value: '股份有限公司',
          label: '股份有限公司'
        }],
        dialogLoading:'',
        form: {
          enterpriseName:'',
          legalRepresentative:'',
          establishDate:'',
          registeredCapital:'',
          businessAddress:'',
          classification:'',
          inputDate:'',
          industry:'',
          stockCode:''
        },
        options:[
        ],
        options1:[
        ],
        userVo:{
          account:'',
          password:'123456'
        },
        rules: {
          enterpriseName: [
            { required: true, message: '请输入企业名称', trigger: 'blur' },
          ],
          legalRepresentative: [
            { required: true, message: '请输入法人代表', trigger: 'blur' },
          ],
          establishDate: [
            { required: true, message: '请选择日期', trigger: 'blur' }
          ],
          inputDate: [
            { required: true, message: '请选择时间', trigger: 'blur' }
          ],
          registeredCapital: [
            { required: true, message: '请输入注册资本', trigger: 'blur' }
          ],
          businessAddress: [
            { required: true, message: '请输入办公地址', trigger: 'blur' }
          ],
          classification: [
            { required: true, message: '请选择工商分类', trigger: 'blur' }
          ],
          industry: [
            { required: true, message: '请选择主要行业', trigger: 'blur' }
          ]
        },
        twoUP:false,
        dialogId:0
      }
    },
    watch: {
      AddId: {
        deep: true,
        handler(val){
          console.log(val)
        }
      }
    },
    computed: {
      handleScreenWidthToVw() {
        return handleScreenWidthToVw
      },
    },
    created() {
      this.getUpData()
    },
    methods: {
      handleClose() {
        this.resetForm('form')
        this.$emit('close')
      },
      success(form){
        this.$refs[form].validate((valid) => {
          if (valid) {
            if(!this.AddId) {
              AddEnterprise({ ...this.form }).then(res => {
                if(res) {
                  this.$message({
                    message: '新增成功！',
                    type: 'success'
                  })
                  this.$parent.getTableData()
                }
              })
            }else{
              UpdateEnterprise({ ...this.form }).then(res => {
                if(res) {
                  this.$message({
                    message: '修改成功！',
                    type: 'success'
                  })
                }
              })
              this.$parent.getTableData()
              // const updateRes = updateUserOrPassword(this.userVo)
              // console.log(updateRes)
            }
            this.$emit('close')
            } else {
                return false;
            }
        });
      },
      parentHandleclick(id) {
        this.dialogLoading = true
        ApiEnterpriseInfo({enterpriseId: id}).then(res => {
          this.form = res
          this.userVo.account = res.account
          this.userVo.password = res.password
          this.dialogLoading = false
        })
      },

      // 获取行业下拉列表
      getUpData() {
        getUpDataList({id:this.dialogId}).then((res) => {
          console.log(res);
          this.options = res
        })
      },
  // 点击第一个下拉框 获取第二个下啦框数据
      sureChange(val) {
          getUpDataList({id:val}).then((res) => {
            console.log(res)
            if(res.length === 0){
                this.twoUP = false
            }else{
              this.twoUP = true
                this.options1 = res
              if(this.form.mainIndustryId === '0') {
                this.form.mainIndustryId = this.options1[0].id 
              }
            }
        })
      },
    }
  }
</script>

<style scoped lang="scss">
::v-deep .el-input-group__append{color:#ccc}
</style>
